package com.wanmei.service.Impl;

import com.wanmei.exception.WanmeiException;
import com.wanmei.mapper.AttributeMapper;
import com.wanmei.mapper.CategoryMapper;
import com.wanmei.mapper.ProductMapper;
import com.wanmei.mapper.TransportMapper;
import com.wanmei.pojo.Category;
import com.wanmei.pojo.Exception_Msg;
import com.wanmei.pojo.Product;
import com.wanmei.pojo.UITreeView;
import com.wanmei.service.CategoryService;
import org.apache.shiro.cache.CacheManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private AttributeMapper attributeMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private TransportMapper transportMapper;

    static List<Category> childCategort = new ArrayList<>();

    @Override
    public List<Category> getCategoryByCid(Integer cid) {
        return categoryMapper.selectByCid(cid);
    }


    /**
     * TreeView获取父节点
     *
     * @return
     */
    public List<UITreeView> getParentNode() {
        List<UITreeView> treeViewList = new ArrayList<>();
        List<Category> list = categoryMapper.getALLbyParentId(0);
        //循环遍历父节点 最高级
        for (Category Category : list) {
            UITreeView treeViewNode = new UITreeView();
            treeViewNode.setParentId(Category.getParentid());
            treeViewNode.setText(Category.getCname());
            treeViewNode.setCid(Category.getCid());
            List<Category> list1 = categoryMapper.getALLbyParentId(Category.getCid());
            //将子节点添加到集合
            //开始从父节点的时候，传入最顶层的 UITreeView 对象 还有将第二层遍历循环出来
            getChildByParentsId(treeViewNode, list1);
            treeViewList.add(treeViewNode);
        }
        //获取子节点
        return treeViewList;
    }

    /**
     * 通过递归循环一直获取子节点
     *
     * @param treeView
     * @param listCategory
     */
    public void getChildByParentsId(UITreeView treeView, List<Category> listCategory) {
        for (Category Category : listCategory) {
            //通过UITreeView 的 cid 去判断是不是下一层子节点的ParentsId  如果是的话 重复父节点的操作。一直递归····
            if (treeView.getCid().equals(Category.getParentid())) {
                UITreeView treeView1 = new UITreeView();
                treeView1.setCid(Category.getCid());
                treeView1.setText(Category.getCname());
                treeView1.setParentId(Category.getParentid());
                List<Category> list = categoryMapper.getALLbyParentId(treeView1.getCid());
                getChildByParentsId(treeView1, list);
                treeView.getNodes().add(treeView1);
            }
        }
    }

    @Override
    public int insert(Category category) {
        Date date = new Date();
        SimpleDateFormat format =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowTime = format.format(date);
        category.setAddtime(nowTime);
        return categoryMapper.insert(category);
    }

    /**
     * 删除操作
     *
     * @param cid
     * @return
     */
    public int deleteCategoryByCid(int cid) {
        //先把product attribute里面的产品置空
        productMapper.saveCidNullByCidDelete(cid);
        attributeMapper.updateByCid(cid);
        //删除运费
        transportMapper.deleteByCid(cid);
        //删除分类 ，然后将其直接之类进行置0
        categoryMapper.updateParentsIdByCid(cid);
        return categoryMapper.deleteByPrimaryKey(cid);
    }

    /**
     * 点击模态框获取下拉的数据值,递归所有子节点,然后有子节点的不能进行分类.
     */

    public List<Category> getAllChild(int cid) {

        List<Category> categoryList = categoryMapper.selectAll();
        //获取所有子节点
        List<Category> childCategory = treeCategortList(categoryList, cid);
        //去重复
        List<Category> categoryList1 = new ArrayList<>();
        categoryList1.addAll(categoryList);
        categoryList1.removeAll(childCategory);
        return categoryList1;
    }

    /**
     * 获取某个父节点下面的所有子节点
     *
     * @param
     * @param
     * @return
     */
    public static List<Category> treeCategortList(List<Category> categoryList, int parentid) {
        for (Category cg : categoryList) {
            //遍历出父id等于参数的id，add进子节点集合
            if (cg.getParentid() == parentid) {
                //递归遍历下一级
                treeCategortList(categoryList, cg.getCid());
                childCategort.add(cg);
            }
        }
        return childCategort;
    }

    /**
     * Cid 用户信息
     */
    public Category selectByPrimaryKey(Integer cid) {
        return categoryMapper.selectByPrimaryKey(cid);
    }

    /**
     * 判断是否重名
     */
    public int countCname(String cname) {
    return categoryMapper.countCname(cname);
    }

    /**
     * 编辑操作
     */
    public int saveByPrimaryKeySelective(Category category)throws WanmeiException {
        //如果分类名改变 那么直接子类进行置0处理
        Category category1 = categoryMapper.selectByPrimaryKey(category.getCid());
        if (category1.getCname() == null || category1.getCname() == "") {
            throw new WanmeiException(Exception_Msg.ERROR_300010);
        }
        if (!category1.getCname().equals(category.getCname())) {
            categoryMapper.updateParentsIdByCid(category.getCid());
        }
        if(category.getCid()==category.getParentid()){
            throw new WanmeiException(Exception_Msg.ERROR_300011);
        }
        Date date = new Date();
        SimpleDateFormat format =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowTime = format.format(date);
        category.setModifytime(nowTime);
        return categoryMapper.updateByPrimaryKeySelective(category);
    }



}
